
//
// 120120 - AH - Created.
// 120127 - AH - (Garage) Fixed renaming of factors.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing.ptable;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.soekrat.AttributeObject;
import org.soekrat.engine.SoekratName;
import org.soekrat.engine.ontology.ptable.Product;
import org.soekrat.engine.ontology.ptable.ProductTable;
import org.soekrat.engine.ontology.ptable.ProductTableFactor;

/**
 * <p>
 *   {@link TableModel} that adapts a
 *   {@link ProductTable} to a
 *   {@link JTable} view.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class ProductTableTableModel extends AbstractTableModel 
{
  private final ProductTable m_ptable;
  
  /**
   * <p>
   *   Creates the empty model.
   * </p>
   */
  public ProductTableTableModel(){ m_ptable=null; }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param pt {@link ProductTable}.
   */
  public ProductTableTableModel(ProductTable pt)
  {
    if (pt==null)
    {
      throw new NullPointerException("ptable");
    }
    
    m_ptable=pt;
  }
  
  public void removeRow(ProductTableFactor f)
  {
    m_ptable.removeRowFactor(f);
    fireTableStructureChanged();
  }
  
  public void removeCol(ProductTableFactor f)
  {
    m_ptable.removeColFactor(f);
    fireTableStructureChanged();
  }
  
  /**
   * <p>
   *   Creates row.
   * </p>
   */
  public void addRow()
  {
    if (m_ptable==null)
    {
      throw new IllegalStateException("empty model");
    }
    
    int n=m_ptable.getRowFactorCount();
    String name="Row Factor "+n;
    m_ptable.createRowFactor(name);
    
    fireTableStructureChanged();
  }
  
  /**
   * <p>
   *   Creates column.
   * </p>
   */
  public void addColumn()
  {
    if (m_ptable==null)
    {
      throw new IllegalStateException("empty model");
    }
    
    int n=m_ptable.getColFactorCount();
    String name="Column Factor "+n;
    m_ptable.createColFactor(name);
    
    fireTableStructureChanged();
  }
  
  // Specified in TableModel.
  public String getColumnName(int col){ return " "; }

  // Specified in TableModel.
  public Class<?> getColumnClass(int col) 
  {
    return AttributeObject.class;
  }
  
  // Specified in TableModel.
  public boolean isCellEditable(int row, int col) 
  {
    return row==0 && col==0 ? false : true;
  }

  // Specified in TableModel.
  public int getColumnCount() 
  {
    return m_ptable==null ? 0 : m_ptable.getColFactorCount()+1;
  }

  // Specified in TableModel.
  public int getRowCount() 
  {
    return m_ptable==null ? 0 : m_ptable.getRowFactorCount()+1;
  }

  // Specified in TableModel.
  public Object getValueAt(int row, int col) 
  {
    if (m_ptable==null) 
    {
      throw new IndexOutOfBoundsException();
    }
    
    if (row==0 && col==0)
    {
      return null;
    }
    else if (row==0)
    {
      return m_ptable.getColFactor(col-1);
    }
    else if (col==0)
    {
      return m_ptable.getRowFactor(row-1);
    }
    else
    {
      return m_ptable.getProduct(row-1, col-1);
    }
  }

  // Specified in TableModel.
  public void setValueAt(Object val, int row, int col)
  {
    String name=val.toString();
    
    name=name.trim();
    if (name.length()==0) return;
    
    if (row==0 && col==0)
    {
      return;
    }
    else if (row==0)
    {
      ProductTableFactor f=m_ptable.getColFactor(col-1);
      
      SoekratName oldf=f.getSoekratName();
      SoekratName newf=new SoekratName(name);
      
      m_ptable.changeColFactor(oldf, newf);
      this.fireTableStructureChanged(); // Overdoing it? TODO
    }
    else if (col==0)
    {
      ProductTableFactor f=m_ptable.getRowFactor(row-1);
      
      SoekratName oldf=f.getSoekratName();
      SoekratName newf=new SoekratName(name);
      
      m_ptable.changeRowFactor(oldf, newf);
      this.fireTableStructureChanged(); // Overdoing it? TODO
    }
    else
    {
      int r=row-1;
      int c=col-1;
      Product p=m_ptable.getProduct(r, c);
      if (p==null)
      {
        ProductTableFactor rowf=m_ptable.getRowFactor(r);  
        ProductTableFactor colf=m_ptable.getColFactor(c);
        m_ptable.create(rowf, colf, new SoekratName(name));
        
        this.fireTableCellUpdated(r, c);
      }
      else
      {
        p.setSoekratName(name);
      }
    }
  }
  
}
